package net.csdn.business.common.yidun.text.v5;

import com.netease.yidun.sdk.antispam.AntispamRequester;
import com.netease.yidun.sdk.antispam.text.TextClient;
import com.netease.yidun.sdk.antispam.text.v5.check.sync.single.TextCheckRequest;
import com.netease.yidun.sdk.antispam.text.v5.check.sync.single.TextCheckResponse;
import com.netease.yidun.sdk.antispam.text.v5.check.sync.single.TextCheckResult;
import net.csdn.business.common.yidun.Abstract;
import net.csdn.business.common.yidun.AsyncTextResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 文本单次同步检测
 */
public class TextSingleSyncCheckUtil extends Abstract {

    public static AsyncTextResponse textSingleSyncCheck(String secretId, String secretKey, String businessId, String dataId, String content) {
        AsyncTextResponse asyncResponse = new AsyncTextResponse();
        // 实例化一个requester，入参需要传入易盾内容安全分配的secretId，secretKey
        AntispamRequester antispamRequester = createAntispamRequester(secretId, secretKey);

        // 实例化发起请求的client对象
        TextClient textClient = TextClient.getInstance(antispamRequester);

        // 实例化请求对象
        TextCheckRequest checkRequest = new TextCheckRequest();
        // 设置易盾内容安全分配的businessId
        checkRequest.setBusinessId(businessId);

        // 根据需要设置请求的检测节点，默认杭州，现阶段只支持检测接口
        checkRequest.setRegionCode("cn-hangzhou");

        // 设置检测内容
        checkRequest.setDataId(dataId);
        checkRequest.setContent(content);

        // 请求对象中的其他参数如果有需要，请参考官方接口文档中字段说明，按需添加

        TextCheckResponse checkResponse = null;
        try {
            // 发起同步检测的请求
            checkResponse = textClient.syncCheckText(checkRequest);
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (checkResponse != null && checkResponse.getCode() == 200) {
            // 获取文本的检测结果，具体返回字段的说明，请参考官方接口文档中字段说明
            TextCheckResult textResult = checkResponse.getResult();
            if (textResult == null || textResult.getAntispam() == null) {
                throw new IllegalArgumentException("易盾文本检测返回异常");
            }
            TextCheckResult.Antispam antispam = textResult.getAntispam();
            asyncResponse.setSuggestion(antispam.getSuggestion());
            asyncResponse.setDataId(antispam.getDataId());
            asyncResponse.setTaskId(antispam.getTaskId());
            //建议动作，0：通过，1：嫌疑，2：不通过
            if (antispam.getSuggestion() == 0) {
                return asyncResponse;
            }
            List<TextCheckResult.AntispamLabel> labels = antispam.getLabels();
            if (!CollectionUtils.isEmpty(labels)) {
                for (TextCheckResult.AntispamLabel label : labels) {
                    List<TextCheckResult.AntispamSubLabel> subLabels = label.getSubLabels();
                    if (!CollectionUtils.isEmpty(subLabels)) {
                        for (TextCheckResult.AntispamSubLabel subLabel : subLabels) {
                            TextCheckResult.AntispamSubLabelDetail detail = subLabel.getDetails();
                            if (detail != null) {
                                List<TextCheckResult.AntispamSubLabelDetailHitInfo> hitInfos = detail.getHitInfos();
                                if (!CollectionUtils.isEmpty(hitInfos)) {
                                    for (TextCheckResult.AntispamSubLabelDetailHitInfo hitInfo : hitInfos) {
                                        putAsyncResponse(asyncResponse, hitInfo.getValue(), hitInfo.getPositions());
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return asyncResponse;
        }
        throw new IllegalArgumentException("易盾文本检测接口异常");
    }

    private static void putAsyncResponse(AsyncTextResponse asyncResponse, String value, List<TextCheckResult.AntispamSubLabelDetailHitInfoPosition> positions) {
        if (!StringUtils.isEmpty(value) && !CollectionUtils.isEmpty(positions)) {
            if (CollectionUtils.isEmpty(asyncResponse.getValues())) {
                List<String> values = new ArrayList<>();
                values.add(value);
                asyncResponse.setValues(values);
                asyncResponse.setPositions(positions);
            } else {
                asyncResponse.getValues().add(value);
                asyncResponse.getPositions().add(positions.get(0));
            }
        }
    }
}
